package edu.xpu.compute.recommend.restful;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import edu.xpu.compute.recommend.common.model.PageModel;
import edu.xpu.compute.recommend.common.model.RequestListParam;
import edu.xpu.compute.recommend.common.model.ResponseParam;
import edu.xpu.compute.recommend.entity.SysUserBookRating;
import edu.xpu.compute.recommend.entity.UserBookUnionPk;
import edu.xpu.compute.recommend.service.SysUserBookRatingService;

@RestController
@RequestMapping(value="restful/rating")
public class SysUserBookRatingRestful {
	@Autowired
	private SysUserBookRatingService bookRatingService;

	/**
	 * 获取分页列表信息
	 * @param requestListParam
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@PostMapping(value="list")
	public ResponseParam<PageModel<SysUserBookRating>> list(@RequestBody(required=false) RequestListParam<SysUserBookRating> requestListParam) {
		if (requestListParam == null) {
			requestListParam = new RequestListParam<>();
		}
		PageModel<SysUserBookRating> pageByConditions = bookRatingService.getPageByConditions(requestListParam.getNumber()
													,requestListParam.getSize()
													,requestListParam.getEntity()
													,requestListParam.getSortField()
													,requestListParam.getSortType());
		return new ResponseParam<PageModel<SysUserBookRating>>("数据获取成功", 200, pageByConditions);
	}
	/**
	 * 根据id获取数据
	 * @param id
	 * @return
	 */
	@PostMapping(value="get")
	public ResponseParam<SysUserBookRating> get(@RequestBody UserBookUnionPk id) {
		SysUserBookRating SysUserBookRating = bookRatingService.get(id);
		return new ResponseParam<SysUserBookRating>("数据获取成功", 200, SysUserBookRating);
	}
	/**
	 * 保存数据
	 * @param SysUserBookRating
	 * @return
	 */
	@PostMapping(value="save")
	public ResponseParam<SysUserBookRating> save(@RequestBody SysUserBookRating sysUserBookRating) {
		sysUserBookRating.setCreateDate(new Date());
		SysUserBookRating user = bookRatingService.save(sysUserBookRating);
		return new ResponseParam<SysUserBookRating>("数据保存成功", 200, user);
	}
	/**
	 * 更新数据
	 * @param SysUserBookRating
	 * @return
	 */
	@PostMapping(value="update")
	public ResponseParam<SysUserBookRating> update(@RequestBody SysUserBookRating SysUserBookRating) {
		if (SysUserBookRating == null) {
			return new ResponseParam<>("待更新数据没有主键", 500);
		}
		SysUserBookRating user = bookRatingService.save(SysUserBookRating);
		return new ResponseParam<SysUserBookRating>("数据更新成功", 200, user);
	}
	/**
	 * 删除数据
	 * @param id
	 * @return
	 */
	@PostMapping(value="delete")
	public ResponseParam<String> delete(@RequestBody List<UserBookUnionPk> ids) {
		try {
			for (UserBookUnionPk userBookUnionPk : ids) {
				bookRatingService.delete(userBookUnionPk);
			}
		} catch (Exception e) {
			return new ResponseParam<String>(e.getMessage(), 500);
		}
		return new ResponseParam<String>("数据删除成功", 200);
	}
}
